<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                               |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//
namespace AuctionBack\Controller;

use AuctionCore\Common\AjaxPage;
use AuctionCore\System\SystemLog;

/**
 * 兑换积分订单管理
 *
 * @author Xmh
 */
class UserExchangeController extends BaseController
{
    private $userExchangeLogic;

    private $empId;

    public function __construct()
    {
        parent::__construct();

        $this->userExchangeLogic = D('UserExchange', 'Logic');

        $this->empId = $this->user_info['emp_id'];
    }

    public function index()
    {
        $this->display();
    }

    /**
     * 兑换积分订单列表
     */
    public function userExchangeList()
    {
        $page = I("get.p", 1);
        $size = 10;
        $params = [];
        $params['order_no'] = I('get.order_no', '', 'trim');
        $params['user_id'] = I('get.uid', '', 'trim');
        $params['user_name'] = I('get.user_name', '', 'trim');
        $params['start_time'] = I('get.start_time', '');
        $params['end_time'] = I('get.end_time', '');
        $params['return_start_time'] = I('get.return_start_time', '');
        $params['return_end_time'] = I('get.return_end_time', '');
        $params['status'] = I('get.status/d', 0);
        $result = $this->userExchangeLogic->getUserExchangeList($page, $size, $params);
        $list = !empty($result['list']) ? $result['list'] : [];
        $Page = new AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %nextPage% %downPage%  %end% %jumpform%");
        $show = $Page->show();          // 分页显示输出
        $this->assign('page', $show);   // 赋值分页输出
        $this->assign('list', $list);
        $this->assign('p', $page);
        $this->assign('total', $result['total']);
        $this->assign('params', $params);
        $this->display();
    }

    /**
     * 兑换积分订单详情
     */
    public function userExchangeInfo()
    {
        $id = I('get.id/d', 0);
        if ($id < 1) {
            $this->redirect('ConvertIntegralOrder/index', '', 1, '请求出错了');
        }
        $info = $this->userExchangeLogic->getUserExchangeInfo($id);
        $this->assign('detail', $info);
        $this->display();
    }

    /**
     * 兑换积分订单详情
     */
    public function userExchangeInfoJson()
    {
        $id = I('post.id/d', 0);
        $info = $this->userExchangeLogic->getUserExchangeInfo($id);
        echo json_encode($info);
    }

    /**
     * 退兑换积分订单操作
     */
    public function returnUserExchange()
    {
        $id = I('post.id/d', 0);
        if ($id < 1) {
            $this->helpJsonResult('400', '请求参数错误');
        }
        $info = $this->userExchangeLogic->getUserExchangeInfo($id);
        $data['id'] = $id;
        $data['emp_id'] = $this->empId;
        $data['status'] = 2;
        $data['return_time'] = time();
        $user['user_id'] = $info['user_id'];
        $user['score'] = $info['score'];
        $res = $this->userExchangeLogic->returnUserExchangeOrder($data, $user);
        if (!$res) {
            $this->helpJsonResult('400', '退回操作失败');
        } else {
            SystemLog::add(['emp_id' => $this->empId, 'action' => '积分兑换退回积分']);
            $this->helpJsonResult('200', '退回操作成功');
        }
    }
}